package thread;

/**
 * 并发安全的api
 * StringBuilder和StringBuffer都是用于修改字符串的工具类
 * 而StringBuffer是并发安全的，但是StringBuilder不是
 */
public class SyncApiDemo {
    public static void main(String[] args) {
//        StringBuilder sb = new StringBuilder();
        StringBuffer sb = new StringBuffer();
        Thread t1 = new Thread(){
            public void run(){
                for (int i = 0; i < 10000; i++) {
                    sb.append("abc");
                }
            }
        };
        Thread t2 = new Thread(){
            public void run(){
                for (int i = 0; i < 10000; i++) {
                    sb.append("def");
                }
            }
        };
        t1.start();
        t2.start();
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println(sb.length());
    }
}
